﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiExplorerSettings(GroupName = "v1")]
    [ApiController]
    [Authorize]
    public class DeviceController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public DeviceController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
        )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 查询设备类型
        /// </summary>
        [HttpPost]
        [Route("QueryDevType")]
        public ResponseMessage QueryDevType()
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                message = BUS.QueryDevType();
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询设备类型";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询设备类型异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 新建设备
        /// </summary>
        [HttpPost]
        [Route("AddDevice")]
        public ResponseMessage AddDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                deviceInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID").ToString());
                message = BUS.AddDevice(deviceInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增设备";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增设备异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改设备
        /// </summary>
        [HttpPost]
        [Route("UpdateDevice")]
        public ResponseMessage UpdateDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                message = BUS.EditDevice(deviceInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改设备";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改设备信息异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        [HttpPost]
        [Route("DeleteDevice")]
        public ResponseMessage DeleteDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                message = BUS.DeleteDevice(deviceInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除设备";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除设备异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询设备
        /// </summary>
        [HttpPost]
        [Route("QueryDevice")]
        public ResponseMessage QueryDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                deviceInfo.projectID= Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryDevice(deviceInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询所有设备";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询设备异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 设备列表信息
        /// </summary>
        [HttpPost]
        [Route("QueryDeviceList")]
        public ResponseMessage QueryDeviceList(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                deviceInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryDeviceList(deviceInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询设备列表";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询设备异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入设备信息
        /// </summary>
        [HttpPost]
        [Route("LoadDevice")]
        public ResponseMessage LoadDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                message = BUS.LoadDevice(deviceInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入设备";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入设备异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="parameter">参数内容</param>
        /// <param name="deviceid">设备编号</param>
        [HttpPost]
        [Route("ParameterSet")]
        public ResponseMessage ParameterSet(DeviceParameter deviceParameter, string deviceid)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            ParameterSet parameter = new ParameterSet();
            parameter.fileServerUrl = deviceParameter.fileServerUrl;
            parameter.httpUserName = deviceParameter.httpUserName;
            parameter.httpPassword = deviceParameter.httpPassword;
            parameter.ntpServer = deviceParameter.ntpServer;
            parameter.liftCarNumber = deviceParameter.liftCarNumber.ToString();
            parameter.lightMode = deviceParameter.lightMode.ToString();
            parameter.workMode = deviceParameter.workMode.ToString();
            message = BUS.ParameterSet(parameter, deviceid);
            return message;
        }

        /// <summary>
        /// 查询面板设备
        /// </summary>
        [HttpPost]
        [Route("QueryTKDevice")]
        public ResponseMessage QueryTKDevice()
        {
            ResponseMessage message = new ResponseMessage();
            DeviceBUS BUS = new DeviceBUS();
            try
            {
                message = BUS.QueryTKDevice();
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询梯控设备";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询梯控设备异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
